#include "localsetting.h"
#include <QCoreApplication>
#include <QFile>
#include <QDebug>

LocalSetting* LocalSetting::ins = nullptr;
LocalSetting* LocalSetting::instance()
{
    if (ins == nullptr)
    {
        ins = new LocalSetting();
    }
    return ins;
}


bool LocalSetting::isSettingExists()
{
    QString filename = QCoreApplication::applicationDirPath() + "/config.ini";
    return QFile::exists(filename);
}

LocalSetting::LocalSetting()
    : QObject()
    , settings(nullptr)
{
    qDebug() << "LocalSetting " << QCoreApplication::applicationDirPath();
    QString filename = QCoreApplication::applicationDirPath() + "/config.ini";
    if (QFile::exists(filename))
    {
        settings = new QSettings(filename, QSettings::IniFormat, this);
    }
    else
    {
        settings = new QSettings(filename, QSettings::IniFormat, this);
        settings->setValue("lastcount", 0);
        settings->setValue("lasttime", QDateTime::currentDateTime());
        settings->setValue("useface", false);
        settings->setValue("facecount", 0);
    }
}

QDateTime LocalSetting::getLastLoginDatetime()
{
    return settings->value("lasttime").toDateTime();
}

void LocalSetting::setLastLoginDatetime(const QDateTime& time)
{
    settings->setValue("lasttime", time);
    settings->sync();
}

quint32 LocalSetting::getLastLoginFailedCount()
{
    return settings->value("lastcount").toUInt();
}

void LocalSetting::setLastLoginFailedCount(quint32 count)
{
    settings->setValue("lastcount", count);
    settings->sync();
}

void LocalSetting::removeLastloginDatetimeCount()
{
    settings->remove("lasttime");
    settings->remove("lastcount");
    settings->sync();
}

bool LocalSetting::getFaceAuthUsed()
{
    return settings->value("useface").toBool();
}

void LocalSetting::setFaceAuthUsed(bool used)
{
    settings->setValue("useface", used);
    settings->sync();
}

QList<qint32> LocalSetting::getFaceLabelsList() const
{
    QList<qint32> list;
    QList<QVariant> listVariant = settings->value("facelabel").toList();
    for (const QVariant& v : listVariant)
    {
        list.append(v.toInt());
    }
    return list;
}

void LocalSetting::setFaceLabelsList(const QList<qint32>& labelList)
{
    QList<QVariant> listVariant;
    for (qint32 v : labelList)
    {
        listVariant.append(v);
    }
    settings->setValue("facelanel", listVariant);
    settings->sync();
}

QString LocalSetting::getFaceDetectFilepath() const
{
    return QCoreApplication::applicationDirPath() + "/.var.tmpfile";
//    return "D:\\my_libs\\opencv-4.4.0\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml";
//    return "D:\\my_libs\\opencv-4.4.0\\data\\haarcascades\\haarcascade_frontalface_alt2.xml";
}

QList<QString> LocalSetting::getFaceRecogFilepath() const
{
    QList<QString> lists = {
        QCoreApplication::applicationDirPath() + "/.var.ae.profile",
        QCoreApplication::applicationDirPath() + "/.var.gx.profile",
        QCoreApplication::applicationDirPath() + "/.var.mp.profile",
        QCoreApplication::applicationDirPath() + "/.var.oc.profile",
        QCoreApplication::applicationDirPath() + "/.var.wv.profile"
    };
    return lists;
}

quint32 LocalSetting::getLastFaceauthFailedCount()
{
    return settings->value("facecount").toUInt();
}

void LocalSetting::setLastFaceauthFailedCount(quint32 count)
{
    settings->setValue("facecount", count);
    settings->sync();
}

void LocalSetting::removeLastFaceauthDatetimeCount()
{
    settings->remove("facecount");
    settings->sync();
}
